<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <title>Redis学习笔记——订阅发布 - 陈的错题集</title><meta name="Description" content="陈的错题集 - 个人笔记"><meta property="og:title" content="Redis学习笔记——订阅发布" />
<meta property="og:description" content="最近突发奇想，想学习以下Redis作为Scada软件的传输层功能，使用它解耦数据网关与界面，所以主要学习的是redis的订阅发布功能。当然，" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://example.com/redis_01pubsub/" /><meta property="og:image" content="https://example.com/logo.png"/><meta property="article:section" content="posts" />
<meta property="article:published_time" content="2023-08-26T16:39:26+08:00" />
<meta property="article:modified_time" content="2023-08-26T16:39:26+08:00" /><meta property="og:site_name" content="陈的错题集" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://example.com/logo.png"/>

<meta name="twitter:title" content="Redis学习笔记——订阅发布"/>
<meta name="twitter:description" content="最近突发奇想，想学习以下Redis作为Scada软件的传输层功能，使用它解耦数据网关与界面，所以主要学习的是redis的订阅发布功能。当然，"/>
<meta name="application-name" content="陈的错题集">
<meta name="apple-mobile-web-app-title" content="陈的错题集"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://example.com/redis_01pubsub/" /><link rel="prev" href="https://example.com/ethercat_02importantpoint/" /><link rel="next" href="https://example.com/ethercat_03protocol/" /><link rel="stylesheet" href="/css/style.min.css"><link rel="preload" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/all.min.css"></noscript><link rel="preload" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
        <noscript><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@4.1.1/animate.min.css"></noscript><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "Redis学习笔记——订阅发布",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/example.com\/redis_01pubsub\/"
        },"image": ["https:\/\/example.com\/images\/Apple-Devices-Preview.png"],"genre": "posts","keywords": "Redis","wordcount":  1784 ,
        "url": "https:\/\/example.com\/redis_01pubsub\/","datePublished": "2023-08-26T16:39:26+08:00","dateModified": "2023-08-26T16:39:26+08:00","publisher": {
            "@type": "Organization",
            "name": "xxxx","logo": "https:\/\/example.com\/images\/avatar.png"},"author": {
                "@type": "Person",
                "name": "Jacob Chen"
            },"description": ""
    }
    </script></head>
    <body data-header-desktop="fixed" data-header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('auto' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'auto' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="陈的错题集">💻陈的错题集</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 📜归档 </a><a class="menu-item" href="/tags/"> 🏷️标签 </a><a class="menu-item" href="/categories/"> 📂分类 </a><a class="menu-item" href="/about/"> ⭐关于 </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder=" " id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
                </a></div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="陈的错题集">💻陈的错题集</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder=" " id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw" aria-hidden="true"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw" aria-hidden="true"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin" aria-hidden="true"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">📜归档</a><a class="menu-item" href="/tags/" title="">🏷️标签</a><a class="menu-item" href="/categories/" title="">📂分类</a><a class="menu-item" href="/about/" title="">⭐关于</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw" aria-hidden="true"></i>
            </a></div>
    </div>
</header><div class="search-dropdown desktop">
        <div id="search-dropdown-desktop"></div>
    </div>
    <div class="search-dropdown mobile">
        <div id="search-dropdown-mobile"></div>
    </div><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animate__animated animate__flipInX">Redis学习笔记——订阅发布</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel="author" class="author"><i class="fas fa-user-circle fa-fw" aria-hidden="true"></i>Jacob Chen</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/csharp/"><i class="far fa-folder fa-fw" aria-hidden="true"></i>CSharp</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw" aria-hidden="true"></i>&nbsp;<time datetime="2023-08-26">2023-08-26</time>&nbsp;<i class="fas fa-pencil-alt fa-fw" aria-hidden="true"></i>&nbsp;约 1784 字&nbsp;
                <i class="far fa-clock fa-fw" aria-hidden="true"></i>&nbsp;预计阅读 4 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  data-kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right" aria-hidden="true"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#介绍">介绍</a></li>
    <li><a href="#安装">安装</a>
      <ul>
        <li><a href="#准备资料">准备资料</a></li>
        <li><a href="#安装步骤">安装步骤</a></li>
      </ul>
    </li>
    <li><a href="#redis-订阅发布">Redis 订阅发布</a>
      <ul>
        <li><a href="#简单测试">简单测试</a>
          <ul>
            <li><a href="#nuget引用">NuGet引用</a></li>
            <li><a href="#订阅端subscribe代码">订阅端（Subscribe）代码</a></li>
            <li><a href="#发布端publish代码">发布端（Publish）代码</a></li>
          </ul>
        </li>
        <li><a href="#通过redis发布对象">通过Redis发布对象</a>
          <ul>
            <li><a href="#性能对比">性能对比</a></li>
            <li><a href="#发布对象">发布对象</a></li>
            <li><a href="#订阅端subscribe代码-1">订阅端（Subscribe）代码</a></li>
            <li><a href="#发布端publish代码-1">发布端（Publish）代码</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><!-- # Redis学习笔记——订阅发布 -->
<blockquote>
<p>最近突发奇想，想学习以下Redis作为Scada软件的传输层功能，使用它解耦数据网关与界面，所以主要学习的是redis的订阅发布功能。当然，这也是作为技术路线的储备。</p>
</blockquote>
<h2 id="介绍">介绍</h2>
<p>REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统，是跨平台的非关系型数据库。</p>
<p>Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库，并提供多种语言的 API。</p>
<p>Redis 通常被称为数据结构服务器，因为值（value）可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。</p>
<h2 id="安装">安装</h2>
<h3 id="准备资料">准备资料</h3>
<p>redis 3.2.100.msi：<a href="https://github.com/MicrosoftArchive/redis/releases" target="_blank" rel="noopener noreffer ">Github Releases下载地址</a>
<br/>（<em>因为Redis并没有Windows系统的发行版，由微软编译的版本也停留在Redis3.2版本。好在这个版本已经能满足我的要求。</em>）</p>
<p>CSRedisCore 库：<a href="https://github.com/2881099/csredis" target="_blank" rel="noopener noreffer ">Github</a></p>
<p>学习资料：<a href="https://www.runoob.com/redis/redis-intro.html" target="_blank" rel="noopener noreffer ">Redis教程_菜鸟教程</a></p>
<h3 id="安装步骤">安装步骤</h3>
<ol>
<li>
<p>打开 “redis 3.2.100.msi” 然后一直下一步就可以了。（没啥难度）</p>
</li>
<li>
<p>按“win + R”打开运行，输入“services.msc”。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-win%e8%bf%90%e8%a1%8c.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-win%e8%bf%90%e8%a1%8c.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-win%e8%bf%90%e8%a1%8c.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-win%e8%bf%90%e8%a1%8c.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-win运行.png"
        title="打开运行框" /></p>
</li>
<li>
<p>在服务列表中找到“Redis”，将启动类型设置为“自动”，并启动服务</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%90%af%e5%8a%a8%e5%90%8e%e5%8f%b0%e6%9c%8d%e5%8a%a1.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%90%af%e5%8a%a8%e5%90%8e%e5%8f%b0%e6%9c%8d%e5%8a%a1.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%90%af%e5%8a%a8%e5%90%8e%e5%8f%b0%e6%9c%8d%e5%8a%a1.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%90%af%e5%8a%a8%e5%90%8e%e5%8f%b0%e6%9c%8d%e5%8a%a1.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-启动后台服务.png"
        title="启动服务" /></p>
</li>
<li>
<p>找到Redis安装目录，并双击打开“redis-cli.exe”，输入redis命令，测试是否能正常使用。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%ae%89%e8%a3%85%e6%88%90%e5%8a%9f.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%ae%89%e8%a3%85%e6%88%90%e5%8a%9f.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%ae%89%e8%a3%85%e6%88%90%e5%8a%9f.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%ae%89%e8%a3%85%e6%88%90%e5%8a%9f.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-测试安装成功.png"
        title="测试" /></p>
</li>
</ol>
<h2 id="redis-订阅发布">Redis 订阅发布</h2>
<p>Redis 发布订阅 (pub/sub) 是一种消息通信模式：发送者 (pub) 发送消息，订阅者 (sub) 接收消息。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e8%ae%a2%e9%98%85.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e8%ae%a2%e9%98%85.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e8%ae%a2%e9%98%85.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e8%ae%a2%e9%98%85.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-订阅.png"
        title="订阅" /></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%8f%91%e5%b8%83.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%8f%91%e5%b8%83.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%8f%91%e5%b8%83.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%8f%91%e5%b8%83.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-发布.png"
        title="发布" /></p>
<p>基于订阅发布模式，数据网关就可以只是专心读取数据，并把它定时或者数据变化时再推送（Pulish）出去就可以了。</p>
<h3 id="简单测试">简单测试</h3>
<h4 id="nuget引用">NuGet引用</h4>
<blockquote>
<p>nuget Install-Package CSRedisCore</p>
</blockquote>
<h4 id="订阅端subscribe代码">订阅端（Subscribe）代码</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span><span class="lnt">7
</span><span class="lnt">8
</span><span class="lnt">9
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C#" data-lang="C#"><span class="line"><span class="cl"><span class="k">internal</span> <span class="k">class</span> <span class="nc">Program</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="k">void</span> <span class="n">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">rds</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CSRedisClient</span><span class="p">(</span><span class="s">&#34;127.0.0.1:6379&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">rds</span><span class="p">.</span><span class="n">Subscribe</span><span class="p">((</span><span class="s">&#34;test1&#34;</span><span class="p">,</span> <span class="n">msg</span> <span class="p">=&gt;</span> <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">msg</span><span class="p">.</span><span class="n">Body</span><span class="p">)));</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="发布端publish代码">发布端（Publish）代码</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C#" data-lang="C#"><span class="line"><span class="cl"><span class="k">internal</span> <span class="k">class</span> <span class="nc">Program</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="k">void</span> <span class="n">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">rds</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CSRedisClient</span><span class="p">(</span><span class="s">&#34;127.0.0.1:6379&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span> <span class="p">(</span><span class="k">true</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="kt">var</span> <span class="n">msg</span> <span class="p">=</span> <span class="n">Console</span><span class="p">.</span><span class="n">ReadLine</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="n">msg</span> <span class="p">==</span> <span class="s">&#34;exit&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">            <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="k">break</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">rds</span><span class="p">.</span><span class="n">Publish</span><span class="p">(</span><span class="s">&#34;test1&#34;</span><span class="p">,</span> <span class="n">msg</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-CsRedisPubSubEg.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-CsRedisPubSubEg.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-CsRedisPubSubEg.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-CsRedisPubSubEg.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-CsRedisPubSubEg.png"
        title="测试结果" /></p>
<h3 id="通过redis发布对象">通过Redis发布对象</h3>
<p>由于Redis的订阅发布模式，发送的对象只支持String类型，所以用于发布的对象必须先转换为可存储的类型才可以发布。</p>
<h4 id="性能对比">性能对比</h4>
<p>这边测试将对象转化为可以存储的类型的方式有：</p>
<ol>
<li>使用字符（如：分号；）组合各个数据的值，再通过string.Split()分离（已经可以满足我的使用场景了）。</li>
<li>使用Json进行序列化、反序列化。</li>
<li>使用二进制进行序列化、反序列化。（未测试）</li>
</ol>
<p><strong>测试效果如下：</strong></p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%ba%8f%e5%88%97%e5%8c%96%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%ba%8f%e5%88%97%e5%8c%96%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%ba%8f%e5%88%97%e5%8c%96%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e5%ba%8f%e5%88%97%e5%8c%96%e6%80%a7%e8%83%bd%e5%af%b9%e6%af%94.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-序列化性能对比.png"
        title="性能测试" /></p>
<p><strong>测试代码</strong></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span><span class="lnt">58
</span><span class="lnt">59
</span><span class="lnt">60
</span><span class="lnt">61
</span><span class="lnt">62
</span><span class="lnt">63
</span><span class="lnt">64
</span><span class="lnt">65
</span><span class="lnt">66
</span><span class="lnt">67
</span><span class="lnt">68
</span><span class="lnt">69
</span><span class="lnt">70
</span><span class="lnt">71
</span><span class="lnt">72
</span><span class="lnt">73
</span><span class="lnt">74
</span><span class="lnt">75
</span><span class="lnt">76
</span><span class="lnt">77
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C#" data-lang="C#"><span class="line"><span class="cl"><span class="k">internal</span> <span class="k">class</span> <span class="nc">Program</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="k">void</span> <span class="n">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">sw</span> <span class="p">=</span> <span class="k">new</span> <span class="n">Stopwatch</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">testStrSplit</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NameAndValue2</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="m">1000000</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span> 
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">testStrSplit</span><span class="p">.</span><span class="n">Update</span><span class="p">(</span><span class="s">&#34;Jacob;String;other&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">$&#34;使用string.Split 100W次耗时：{sw.Elapsed}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="kt">string</span> <span class="n">concatStr</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Restart</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="m">1000000</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">concatStr</span> <span class="p">=</span> <span class="n">testStrSplit</span><span class="p">.</span><span class="n">ToString</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">$&#34;使用toString() 100W次耗时：{sw.Elapsed}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">NameAndValue1</span> <span class="n">testJson</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NameAndValue1</span><span class="p">()</span> <span class="p">{</span> <span class="n">Name</span> <span class="p">=</span> <span class="s">&#34;Jacob&#34;</span><span class="p">,</span> <span class="n">Value</span> <span class="p">=</span> <span class="s">&#34;Json&#34;</span><span class="p">,</span> <span class="n">Value2</span> <span class="p">=</span> <span class="s">&#34;other&#34;</span> <span class="p">};</span>
</span></span><span class="line"><span class="cl">        <span class="kt">string</span> <span class="n">json</span> <span class="p">=</span> <span class="kt">string</span><span class="p">.</span><span class="n">Empty</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Restart</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="m">1000000</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">json</span> <span class="p">=</span> <span class="n">JsonConvert</span><span class="p">.</span><span class="n">SerializeObject</span><span class="p">(</span><span class="n">testJson</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">$&#34;使用Json 序列化 100W次耗时：{sw.Elapsed}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">NameAndValue1</span> <span class="n">DeserializeJson</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NameAndValue1</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Restart</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Start</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="p">=</span> <span class="m">0</span><span class="p">;</span> <span class="n">i</span> <span class="p">&lt;</span> <span class="m">1000000</span><span class="p">;</span> <span class="n">i</span><span class="p">++)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">DeserializeJson</span> <span class="p">=</span> <span class="n">JsonConvert</span><span class="p">.</span><span class="n">DeserializeObject</span><span class="p">&lt;</span><span class="n">NameAndValue1</span><span class="p">&gt;(</span><span class="n">json</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">sw</span><span class="p">.</span><span class="n">Stop</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">$&#34;使用Json 反序列化 100W次耗时：{sw.Elapsed}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">class</span> <span class="nc">NameAndValue1</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value2</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">class</span> <span class="nc">NameAndValue2</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value2</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="k">void</span> <span class="n">Update</span><span class="p">(</span><span class="kt">string</span> <span class="n">msg</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="kt">var</span> <span class="n">strs</span> <span class="p">=</span> <span class="n">msg</span><span class="p">.</span><span class="n">Split</span><span class="p">(</span><span class="s">&#34;;&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">Name</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">0</span><span class="p">];</span> 
</span></span><span class="line"><span class="cl">            <span class="n">Value</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">1</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">            <span class="n">Value2</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">2</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="k">override</span> <span class="kt">string</span> <span class="n">ToString</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="kt">string</span><span class="p">.</span><span class="n">Format</span><span class="p">(</span><span class="s">$&#34;{this.Name};{this.Value};{this.Value2}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="发布对象">发布对象</h4>
<h4 id="订阅端subscribe代码-1">订阅端（Subscribe）代码</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C#" data-lang="C#"><span class="line"><span class="cl"><span class="k">internal</span> <span class="k">class</span> <span class="nc">Program</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="k">void</span> <span class="n">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">rds</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CSRedisClient</span><span class="p">(</span><span class="s">&#34;127.0.0.1:6379&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">NameAndValue2</span> <span class="n">name</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NameAndValue2</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="n">rds</span><span class="p">.</span><span class="n">Subscribe</span><span class="p">((</span><span class="s">&#34;test1&#34;</span><span class="p">,</span> <span class="p">(</span><span class="n">msg</span><span class="p">)</span> <span class="p">=&gt;</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">name</span><span class="p">.</span><span class="n">Update</span><span class="p">(</span><span class="n">msg</span><span class="p">.</span><span class="n">Body</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="s">$&#34;name:{name.Name}, value = {name.Value}, value2 = {name.Value2}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}));</span>
</span></span><span class="line"><span class="cl">        <span class="n">Console</span><span class="p">.</span><span class="n">ReadKey</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">class</span> <span class="nc">NameAndValue2</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="kt">string</span> <span class="n">Value2</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="k">void</span> <span class="n">Update</span><span class="p">(</span><span class="kt">string</span> <span class="n">msg</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="kt">var</span> <span class="n">strs</span> <span class="p">=</span> <span class="n">msg</span><span class="p">.</span><span class="n">Split</span><span class="p">(</span><span class="s">&#34;;&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">Name</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">0</span><span class="p">];</span> 
</span></span><span class="line"><span class="cl">            <span class="n">Value</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">1</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">            <span class="n">Value2</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">2</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="k">public</span> <span class="k">override</span> <span class="kt">string</span> <span class="n">ToString</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="kt">string</span><span class="p">.</span><span class="n">Format</span><span class="p">(</span><span class="s">$&#34;{this.Name};{this.Value};{this.Value2}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="发布端publish代码-1">发布端（Publish）代码</h4>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-C#" data-lang="C#"><span class="line"><span class="cl"><span class="k">internal</span> <span class="k">class</span> <span class="nc">Program</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">static</span> <span class="k">void</span> <span class="n">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span> <span class="n">args</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">rds</span> <span class="p">=</span> <span class="k">new</span> <span class="n">CSRedisClient</span><span class="p">(</span><span class="s">&#34;127.0.0.1:6379&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">name</span> <span class="p">=</span> <span class="k">new</span> <span class="n">NameAndValue2</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span> <span class="p">(</span><span class="k">true</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="kt">var</span> <span class="n">msg</span> <span class="p">=</span> <span class="n">Console</span><span class="p">.</span><span class="n">ReadLine</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span> <span class="p">(</span><span class="n">msg</span> <span class="p">==</span> <span class="s">&#34;exit&#34;</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">            <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="k">break</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">name</span><span class="p">.</span><span class="n">Update</span><span class="p">(</span><span class="n">msg</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">rds</span><span class="p">.</span><span class="n">Publish</span><span class="p">(</span><span class="s">&#34;test1&#34;</span><span class="p">,</span> <span class="n">name</span><span class="p">.</span><span class="n">ToString</span><span class="p">())</span> <span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">NameAndValue2</span>
</span></span><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">public</span> <span class="kt">string</span> <span class="n">Name</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">public</span> <span class="kt">string</span> <span class="n">Value</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">public</span> <span class="kt">string</span> <span class="n">Value2</span> <span class="p">{</span> <span class="k">get</span><span class="p">;</span> <span class="k">set</span><span class="p">;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">public</span> <span class="k">void</span> <span class="n">Update</span><span class="p">(</span><span class="kt">string</span> <span class="n">msg</span><span class="p">)</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="kt">var</span> <span class="n">strs</span> <span class="p">=</span> <span class="n">msg</span><span class="p">.</span><span class="n">Split</span><span class="p">(</span><span class="s">&#34;;&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">        <span class="n">Name</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">0</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="n">Value</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">1</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">        <span class="n">Value2</span> <span class="p">=</span> <span class="n">strs</span><span class="p">[</span><span class="m">2</span><span class="p">];</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">public</span> <span class="k">override</span> <span class="kt">string</span> <span class="n">ToString</span><span class="p">()</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="kt">string</span><span class="p">.</span><span class="n">Format</span><span class="p">(</span><span class="s">$&#34;{this.Name};{this.Value};{this.Value2}&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%8f%91%e9%80%81%e5%af%b9%e8%b1%a1.png"
        data-srcset="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%8f%91%e9%80%81%e5%af%b9%e8%b1%a1.png, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%8f%91%e9%80%81%e5%af%b9%e8%b1%a1.png 1.5x, https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-%e6%b5%8b%e8%af%95%e5%8f%91%e9%80%81%e5%af%b9%e8%b1%a1.png 2x"
        data-sizes="auto"
        alt="https://gitee.com/jacobcj/blog-pic/raw/master/blog/Redis-测试发送对象.png"
        title="测试结果" /></p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2023-08-26</span>
            </div></div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/redis_01pubsub/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw" aria-hidden="true"></i>&nbsp;<a href="/tags/redis/">Redis</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/ethercat_02importantpoint/" class="prev" rel="prev" title="EtherCAT从会到不会——重要概念（2）"><i class="fas fa-angle-left fa-fw" aria-hidden="true"></i>EtherCAT从会到不会——重要概念（2）</a>
            <a href="/ethercat_03protocol/" class="next" rel="next" title="EtherCAT从会到不会——封包解析（3）">EtherCAT从会到不会——封包解析（3）<i class="fas fa-angle-right fa-fw" aria-hidden="true"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.115.4">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.11"><i class="far fa-kiss-wink-heart fa-fw" aria-hidden="true"></i> LoveIt</a>
                </div><div class="footer-line" itemscope itemtype="http://schema.org/CreativeWork"><i class="far fa-copyright fa-fw" aria-hidden="true"></i><span itemprop="copyrightYear">2023 - 2024</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">Jacob Chen</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw" aria-hidden="true"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw" aria-hidden="true"></i>
            </a>
        </div><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/autocomplete.js@0.38.1/dist/autocomplete.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lunr@2.3.9/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lazysizes@5.3.2/lazysizes.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/clipboard@2.0.11/dist/clipboard.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":30,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
